var express = require('express');
var getRouter = express.Router();
var postRouter = express.Router();
var axios = require('axios');
var userDao = require('../DAO/userDAO');
const { user } = require('../conf/mysql');
// var fs = require('fs');

var rootApi = "http://112.74.105.17:20003/question-bank"
/* GET users listing. */
getRouter.get('/addWrong', function (req, res, next) {
    // console.log(req.query.data)
    // var thisApi = '/wx/answer/stem'
    // axios.get(rootApi + thisApi + questionId)
    //     .then(function (response) {
    //         res.send(response.data);
    //     })
    //     .catch(function (error) {
    //         console.log(error);
    //     });

});

postRouter.post('/addWrong', function (req, res, next) {
    //租户id 757021031830388736 是我们组的，必选参数，其他可以不要，所以租户id在这里写死了
    // var thisApi = '/wx/answer/stem'
    //查询用户信息-手机号-密码
    var quesId
    var userId
    // var check_item
    console.log("At least iam coming!")
    console.log(JSON.stringify(req.body))
    userId=JSON.stringify(req.body.userId)
    userId=userId.substring(1, (userId.length - 1))
    quesId=JSON.stringify(req.body.quesId)
    quesId=quesId.substring(1, (quesId.length - 1))


    var addWrongSql = "insert into wrongBook(userId,quesId) VALUES('"+userId+"','"+quesId+"')"

    console.log(addWrongSql);
    // userDao.queryAll(checkWrongSql, res, function (check_result) {
    //     var check_item = JSON.stringify(check_result);
    //     console.log(check_item)
    //     res.send(check_item)
    // });
    userDao.add(addWrongSql, res,function(addWrong_flags){
        console.log("addWrong_flags:"+addWrong_flags)
        if(addWrong_flags==1){
            // res.redirect('/loginhhtml');
            console.log("成功添加到错题本")
            res.send("1,成功添加到错题本")
        }else{
            console.log("失败")
            res.send("2,该题已存在")
        }
    });

    // res.send(userName+":"+pword)
    //写到这里，随机返回一道题接口
    // axios.post(rootApi + thisApi, {
    //     gradeName: gradeName,
    //     groupName: groupName,
    //     subject: subject,
    //     tenantId: tenantId
    // })
    //     .then(function (response) {
    //         res.send(response.data)
    //     })
    //     .catch(function (error) {
    //         console.log(error);
    //     });
});

exports.get = getRouter;
exports.post = postRouter;
// module.exports = router;
